package br.com.cast.syscast.services;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.cast.syscast.excecoes.UfJaCadastrada;
import br.com.cast.syscast.models.Pais;
import br.com.cast.syscast.models.Uf;
import br.com.cast.syscast.persistencia.UfDAO;

/**
 * Serviço utilizado nas funcionalidades do Manter UF
 * @author matheus.souza
 *
 */
@Component
public class UfService implements IUfService {
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UfDAO ufDAO;
	
	@Transactional
	@Override
	public void salvar(Uf uf) {
		
		if (ufDAO.ufJaCadastrada(uf)) {
			throw new UfJaCadastrada();
		} else {
			if (uf.getId() == null) {
				ufDAO.salvar(uf);
			} else {
				ufDAO.alterar(uf);
			}
		}
		
	}
	
	@Transactional
	@Override
	public void excluir(Uf uf) {
		ufDAO.excluir(uf);
	}
	
	@Override
	public List<Uf> consultaPorPais(Pais pais) {
		return ufDAO.consultaPorPais(pais);
	}

	@Override
	public void detach() { }


	@Override
	public Iterator<? extends Uf> iterator(long inicio, long count) {
		return ufDAO.consultaPaginada(inicio, count);
	}


	@Override
	public long size() {
		return ufDAO.total();
	}


	@Override
	public IModel<Uf> model(Uf uf) {
		return new CompoundPropertyModel<>(uf);
	}


}
